<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-8-11
 * Time: 下午12:41
 * To change this template use File | Settings | File Templates.
 */
class login extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();

        if ($this->isLogin()) {
            $this->load->helper('url');
            redirect("/");
            return ;
        }
    }

    public function index()
    {
        $referer = $this->input->server('HTTP_REFERER');
        $referer = $referer && strpos($referer, 'login/submit') !== false ? $referer : '';
        $this->load->view('user/login', array('referer'=>$referer));
    }

    public function out()
    {
        $this->load->helper('cookie');
        self::include_uc_lib();
        $redirect_url = '/';
        set_cookie('auth', '', time() - 86400);
        $synlogout = uc_user_synlogout();
        $this->message('注销账户', "成功退出 {$synlogout}" , $redirect_url);
    }

    public function submit()
    {
        $this->load->helper('validation');
        $this->load->helper('cookie');

        $passport = $this->input->get_post('username');
        $password = $this->input->get_post('password');
        $account_type = intval($this->input->get_post('account_type'));
        $redirect_url = $this->input->get_post('redirect_url');
        $redirect_url = is_url($redirect_url) ? $redirect_url : config_item('base_url'); //跳转地址
        $error = '';
        do {

            if (!length_limit($passport, 6, 32)) {
                $error = '用户名不合法';
                break;
            }

            if (!length_limit($password, 6, 32)) {
                $error = '密码长度6~32个字符';
                break;
            }
        } while (false);

        if($error)
        {
            $this->message('登录失败', $error , 'javascript:history.back();');
        }
        else
        {
            self::include_uc_lib();
            $userinfo = uc_user_login($passport, $password, $account_type, '', '', '');
            list($user['user_id'], $user['passport'], $user['nickname'], $user['password'], $user['email'], $duplicate) = $userinfo;
            if($user['user_id'] < 1)
            {
                $this->message('登录失败', '用户名或密码错误' , 'javascript:history.back();');
                return;
            }
            else
            {
                $user['password'] = md5(uniqid('', true));
                //error_reporting(E_ALL);
                $this->load->model('user/Model_User', 'user');
                //$user['password'] = md5($user['password']);
                $uInfo = $this->user->getUserById($user['user_id'], '*');
                if (! $uInfo) {
                    $user['user_status'] = 1;
                    $this->user->registerUser($user, true);
                }
                else
                {
                    (!$uInfo['nickname'] && $user['nickname']) && $this->user->modifyUserNickName($user['user_id'], $user['nickname']);
                }
                //print_r($uInfo);
                $expire = $this->input->get_post('remember') ? time()+(86400 * 360) : 0; //是否记录登录状态
                set_cookie('auth', authcode("{$user['password']}\t{$user['user_id']}", 'ENCODE'), $expire);
                //echo "{$uInfo['password']}\t{$user['user_id']}";
                $synlogin = uc_user_synlogin($user['user_id']);
                $username = $account_type ?  $user['nickname'] : $user['passport'];
                $this->message('登录成功', "{$username} 欢迎您 {$synlogin}" , $redirect_url);
            }
        }
    }
}
